package com.jjb.www.ui.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatCheckBox;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.view.View;

import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.hjq.base.BaseDialog;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.permissions.Permission;
import com.hjq.shape.layout.ShapeLinearLayout;
import com.hjq.shape.layout.ShapeRecyclerView;
import com.hjq.shape.view.ShapeCheckBox;
import com.hjq.shape.view.ShapeImageView;
import com.hjq.shape.view.ShapeTextView;
import com.jjb.www.R;
import com.jjb.www.aop.Permissions;
import com.jjb.www.app.AppActivity;
import com.jjb.www.http.api.PlanDetitleApi;
import com.jjb.www.http.api.PreDetailsApi;
import com.jjb.www.http.glide.GlideApp;
import com.jjb.www.http.model.HttpData;
import com.jjb.www.ui.adapter.PlanDetailsAdapter;
import com.jjb.www.ui.adapter.PreDetailsAdapter;
import com.jjb.www.ui.dialog.MessageDialog;
/**
 * author : Android CRG
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2018/10/18
 * desc   : 岗前计划详情
 */
public class PreEmploymentEducationActivity extends AppActivity {
    private ShapeTextView mTvClassTime;
    private ShapeTextView mTvDay;
    private ShapeImageView mImgPhoto;
    private ShapeTextView mTvMecName;
    private ShapeTextView mTvAddress;
    private ShapeRecyclerView mRvList;
    private ShapeCheckBox mIvPayCheck;
    private ShapeTextView mTvSignUp;
    private PreDetailsAdapter adapter;
    private ShapeImageView mTvCallPhone;
    private ShapeTextView mTvPrice;
    private ShapeLinearLayout mLlPrice;
    private ShapeTextView mTvMessage;





    private PreDetailsApi.Bean.PlanInfoDTO bean;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_pre_employment_education;
    }

    @Override
    protected void initView() {
        mTvMessage = findViewById(R.id.tv_message);
        mTvClassTime = findViewById(R.id.tv_class_time);
        mTvDay = findViewById(R.id.tv_day);
        mTvCallPhone = findViewById(R.id.tv_call_phone);
        mImgPhoto = findViewById(R.id.img_photo);
        mLlPrice = findViewById(R.id.ll_price);
        mTvMecName = findViewById(R.id.tv_mec_name);
        mTvAddress = findViewById(R.id.tv_address);
        mRvList = findViewById(R.id.rv_list);
        mIvPayCheck = findViewById(R.id.iv_pay_check);
        mTvSignUp = findViewById(R.id.tv_sign_up);
        mTvPrice = findViewById(R.id.tv_price);

        adapter = new PreDetailsAdapter(getContext());
        mRvList.setAdapter(adapter);
        mRvList.setLayoutManager(new LinearLayoutManager(getContext()));

        setOnClickListener(mTvSignUp,mTvCallPhone);
    }
    @Permissions({Permission.CALL_PHONE})
    private void callPhone(String phone) {
        Intent intent = new Intent(Intent.ACTION_CALL);
        Uri data = Uri.parse("tel:" + phone);
        intent.setData(data);
        startActivity(intent);
    }

    @Override
    public void onClick(View view) {
        if (view==mTvSignUp){

            if (!mIvPayCheck.isChecked()){
                toast("请勾选报名须知");
                return;
            }
            if (bean.getIs_show()==1){
                new MessageDialog.Builder(getContext())
                        .setMessage("请联系机构报名，联系电话"+bean.getUrgent_mobile())
                        .setConfirm("复制到粘贴板")
                        .setCancel(null)
                        .setListener(new MessageDialog.OnListener() {
                            @Override
                            public void onConfirm(BaseDialog dialog) {
                                toast("复制成功");
                                ClipboardManager cm = (ClipboardManager)getContext() .getSystemService(Context.CLIPBOARD_SERVICE);
                                // 创建普通字符型ClipData
                                ClipData mClipData = ClipData.newPlainText("Label", bean.getUrgent_mobile());
                                // 将ClipData内容放到系统剪贴板里。
                                cm.setPrimaryClip(mClipData);
                            }

                            @Override
                            public void onCancel(BaseDialog dialog) {
                            }
                        })
                        .show();
            }else {
                toast("去支付");
            }
        }else if (view==mTvCallPhone){
            callPhone(bean.getUrgent_mobile());
        }
    }

    @Override
    protected void initData() {
        String time = "我已阅读<font color='#3675FE'>" + "《报名须知》" + "</font>并同意相关内容";
        mTvMessage.setText(Html.fromHtml(time));
        EasyHttp.get(this)
                .api(new PreDetailsApi()
                        .setId(getString("id"))
                        )
                .request(new HttpCallback<HttpData<PreDetailsApi.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpData<PreDetailsApi.Bean> data) {
                        bean = data.getData().getPlan_info();
                        if (bean.getIs_show()==1){
                            mLlPrice.setVisibility(View.GONE);
                        }else {
                            mLlPrice.setVisibility(View.VISIBLE);
                        }
                        mTvClassTime.setText(getString("className"));
                        mTvDay.setText("距离开班时间："+bean.getDay()+"天");
                        mTvPrice.setText(data.getData().getPlan_info().getAndroid_money());
                        GlideApp.with(getContext())
                                .load(bean.getPhoto())
                                .transform(new MultiTransformation<>(new CenterCrop(), new CircleCrop()))
                                .into(mImgPhoto);
                        mTvMecName.setText("机构名称："+bean.getMech_name());
                        mTvAddress.setText("机构地址："+bean.getSchool_address());
                        adapter.setData(data.getData().getPlan_info().getTime_list());
                    }

                });
    }
}